<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model as ThinkModel;

//库存管理---商品表
class Product extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_PRODUCT__';
    protected $name = 'qt_product';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    public static function getProjectBySort($store_id,$sort)
    {
        $log = self::get(['store_id'=>$store_id,'sort'=>$sort]);

        if (!empty($log))
        {
            return true;
        }else{
            return  false;
        }
    }

    public static function getProjectByName($store_id,$name)
    {
        $log = self::get(['store_id'=>$store_id,'name'=>$name]);

        if (!empty($log))
        {
            return true;
        }else{
            return  false;
        }
    }


    /**
     *  商品列表
    **/
    public function productList($store_id = 0,$cate_id = 0,$keyword = '',$limit = 10)
    {
        $where = [];

        //门店ID查询
        if(!empty($store_id))
        {
            $where[] = ['store_id','eq',$store_id];
        }

        //分类查询
        if(!empty($cate_id))
        {
            $where[] = ['goods_category_id','eq',$cate_id];
        }

        //关键词查询
        if(!empty($keyword))
        {
            $where[] = ['name','like','%'.$keyword.'%'];
        }

        $where[] = ['status','eq',1];
        $list  = $this->where($where)
            ->field('id,name,price,img')
            ->order('create_time desc')
            ->paginate($limit)
            ->each(function($item){
                $item['img'] = get_img_path($item['img']);
                return $item;
            });

        return $list->items();
    }

    /**
     *  商品详情
    **/
    public function productInfo($store_id = 0,$product_id)
    {
        $where[] = ['id','eq',$product_id];
        $where[] = ['status','eq',1];
        if(!empty($store_id))
        {
            $where[] = ['store_id','eq',$store_id];
        }

        $info = $this->where($where)->find();
        if($info)
        {
            $info['img'] = get_img_path($info['img']);
        }

        return $info;
    }


}